<template>
  <section class="app-inner">
    <el-card class="header-card">
      <el-form :inline="true" :model="queryParams" size="small" label-width="84px">
        <el-form-item label="部门名称">
          <el-input clearable placeholder="请输入部门名称" v-model="queryParams.deptNameKeywords"></el-input>
        </el-form-item>

        <el-form-item label="部门级别">
          <el-select placeholder="请选择部门级别" v-model="queryParams.deptLevel" filterable :clearable="true">
            <el-option clearable v-for="item in deptLevel" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item class="card-flex">
          <el-button v-if="checkAuthority('/select')" type="primary" @click="searchData">查询</el-button>
          <el-button v-if="checkAuthority('/dept/add')" @click="handleAdd" type="primary" icon="plus" size="small">新增</el-button>
          <div v-if="checkAuthority('/eyes')" class="switch-btn cursor iconfont" :class="[isShow ? 'icon-biyan': 'icon-yanjing']" @click="handleClickIsShow"></div>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="table-card">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="序号" width="100">
          <template slot-scope="scope">
            {{pageSize*(pageNum-1)+scope.$index+1}}
          </template>
        </el-table-column>
        <el-table-column prop="deptNumber" label="部门编号"></el-table-column>
        <!-- <el-table-column prop="eqptAddress" label="设备位置">
          <template slot-scope="scope">
            {{ scope.row.eqptAddress.replace(/null/g, '未知') }}
          </template>
        </el-table-column> -->
        <el-table-column prop="deptName" label="部门名称"></el-table-column>
        <el-table-column prop="deptLevel" label="部门级别">
          <template slot-scope="scope">
          {{
              scope.row.deptLevel === 1
              ? '一级部门'
              : scope.row.deptLevel === 2
              ? '二级部门'
              : scope.row.deptLevel === 3
              ? '三级部门'
              : '其他部门'
          }}
          </template>
        </el-table-column>
        <el-table-column prop="teacherName" label="主管人员"></el-table-column>
         <el-table-column label="主管电话"  width="300px">
          <template v-slot='{row}'>
            {{ !isShow ? $options.filters.cellularPhone(row.teacherPhone) : row.teacherPhone || '--' }}
            <!-- {{row.teacherPhone | cellularPhone}} -->
          </template>
        </el-table-column>
        <el-table-column prop="isEnable" label="是否启用">
          <template slot-scope="scope">
          {{
              scope.row.isEnable === 1
              ? '是'
              : scope.row.isEnable === 0
              ? '否'
              : ''
          }}
          </template>
        </el-table-column>
        <el-table-column prop="parentDeptName" label="上级部门"></el-table-column>
        <el-table-column prop="remark" label="部门描述"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button v-if="checkAuthority('/dept/update')" @click="handleEdit(scope.row)" type="text" size="small">编辑</el-button>
            <el-button v-if="checkAuthority('/dept/delete')" @click="handleDelete(scope.row)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        layout="total, prev, pager, next"
        @current-change="pageHandleChange"
        :current-page="pageNum"
        :page-size="pageSize"
        :total="totalPageNum"
      ></el-pagination>
    </el-card>
    <department-modal ref="departmentModal" @ok="departmentModalOk" v-model="once"></department-modal>
  </section>
</template>

<script>
import { getDepartmentInfoList, deleteDepartmentInfo } from './js/departmentInfo'
import departmentModal from './modules/departmentModal'
import { deptLevel, accountIsEnable } from '@/views/children/auth/js/constants'
import { checkAuthority } from '@/utils/btnPermission' // 页面按钮权限
export default {
  name: 'departmentInfo',
  components: { departmentModal },
  data () {
    return {
      tabPosition: 1,
      pageNum: 1,
      pageSize: 10,
      totalPageNum: 1,
      tableData: [],
      queryParams: {},
      once: 0,
      deptLevel,
      isShow: false
    }
  },
  mounted () {
    this.getDepartmentInfoList()
  },
  methods: {
    checkAuthority,
    handleClickIsShow () {
      this.isShow = !this.isShow
    },
    // 查询按钮
    searchData () {
      this.pageNum = 1
      this.getDepartmentInfoList()
    },
    handleRadioChange () {
      this.pageNum = 1
      this.getDepartmentInfoList()
    },
    // 分页
    pageHandleChange (page) {
      this.pageNum = page
      this.getDepartmentInfoList()
    },
    async getDepartmentInfoList () {
      const params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize
      }
      // this.queryParams,
      const data = Object.assign({}, this.queryParams, params)
      console.log(data)

      let res = await getDepartmentInfoList(data)
      console.log(res)
      this.tableData = res.data.records
      this.totalPageNum = res.data.total
    },
    // 新增
    handleAdd () {
      this.$refs.departmentModal.isAdd = true
      this.$refs.departmentModal.add()
    },
    // 编辑
    handleEdit (row) {
      this.once = 0
      this.$refs.departmentModal.isAdd = false
      this.$refs.departmentModal.edit(row, 0)
    },
    // 删除
    handleDelete (row) {
      this.$confirm('您确定删除当前部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.deleteFun(row)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    async deleteFun (row) {
      console.log(row)
      let res = await deleteDepartmentInfo({ deptId: row.id })
      if (res.code === 0) {
        if (this.tableData.length === 1 && this.pageNum > 1) {
          this.pageNum--
        }
        this.$message.success('删除成功!')
        this.getDepartmentInfoList()
      } else {
        this.$message.error(res.msg)
      }
    },
    departmentModalOk () {
      this.getDepartmentInfoList()
    },
    // 获取label
    getLabel (type, val) {
      if (type === 1) {
        return this.$base.constFormatter(deptLevel, val)
      }
      if (type === 2) {
        return this.$base.constFormatter(accountIsEnable, val)
      }
    }
  }
}
</script>

<style scoped></style>
